import numpy as np
import cv2 as cv

# 读取图像
image_path = "../LingXiao.png"
img = cv.imread(image_path)

# 检查图像是否成功读取
if img is None:
    print(f"Error: Image not found or unable to open the image at path: {image_path}")
    exit(1)

# 图像的基本信息
print(f"Type: {type(img)}")              # 数据类型
print(f"Shape (h, w, c): {img.shape}")   # 图像的高、宽、通道数
print(f"Size (pixels): {img.size}")      # 图像的像素数
print(f"Dtype: {img.dtype}")             # 图像元素的数据类型

# 访问图像的像素信息
x, y = 100, 100
print(f"Pixel value at ({x}, {y}): [B, G, R] = {img[y, x]}")
print(f"B channel value: {img[y, x, 0]}")
print(f"G channel value: {img[y, x, 1]}")
print(f"R channel value: {img[y, x, 2]}")

# 修改图像的某个像素值
img[0, 100] = [0, 0, 255]  # 设置为红色

# 图像裁剪 (y0:y1, x0:x1)
crop_coords = (116, 150, 343, 385)  # [y0, y1, x0, x1]
crop_img = img[crop_coords[0]:crop_coords[1], crop_coords[2]:crop_coords[3]].copy()

# 拆分和合并图像通道
b, g, r = cv.split(img)
img = cv.merge((b, g, r))

# 制作图像边界（填充）
top, bottom, left, right = 50, 50, 10, 10
replicate = cv.copyMakeBorder(img, top, bottom, left, right, cv.BORDER_REPLICATE)

cv.imshow("LingXiao", img)
cv.imshow("Crop Image", crop_img)
cv.imshow("Replicated Border", replicate)

# 等待按键
print("Press ESC to close the windows.")
while True:
    key = cv.waitKey(0)
    if key == 27:  # ESC 键
        break
    else:
        print(f"Key pressed: {key}")

# 关闭所有窗口
cv.destroyAllWindows()
